package com.young.msg.service.impl;

import com.young.common.model.enums.ETrashFlagEnum;
import com.young.common.util.AssertUtil;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.IDGenerator;
import com.young.common.util.Page;
import com.young.interfaces.log.annotation.Log;
import com.young.msg.model.SiteLetter;
import com.young.msg.model.enums.ESiteLetterReceiverTypeEnum;
import com.young.msg.model.enums.ESiteLetterSateEnum;
import com.young.msg.service.ISiteLetterService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 站内信主体服务实现
* Created by wangjiyu on 2021-10-31.
*/
@Service("siteLetterService")
public class SiteLetterServiceImpl implements ISiteLetterService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(SiteLetterServiceImpl.class);

    private static final String MAPPER = "com.young.msg.mapper.siteLetter";//mapper的namespace

    /**
     * 根据发送时间查询待收信件
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Log("根据发送时间查询待收信件(所有人)")
    @Override
    public List<SiteLetter> getReceiveLetterByTime(Date startTime, Date endTime) {
        SiteLetter param = new SiteLetter();
        //指定时间之后的
        param.setSendTime(startTime);
        param.setSendTime2(endTime);
        //有效
        param.setTrashFlag(ETrashFlagEnum.VALID.type);
        //收取发送给所有人的
        param.setReceiverType(ESiteLetterReceiverTypeEnum.ALL.type);
        //已发送状态的
        param.setState(ESiteLetterSateEnum.SENDED.type);
        return dataAccessService.getList(MAPPER + ".getReceiveLetterByTime", param);
    }

    /**
     * 查询用户待收信件(to指定用户)
     *
     * @param userId    收信用户id
     * @param startTime 信件开始时间
     * @param endTime   信件结束时间
     * @return
     */
    @Log("查询用户待收信件(指定收件人)")
    @Override
    public List<SiteLetter> getUserReceiveLetter(String userId, Date startTime, Date endTime) {
        AssertUtil.notBlank(userId, "用户不允许为空");
        SiteLetter param = new SiteLetter();
        //指定时间之后的
        param.setSendTime(startTime);
        param.setSendTime2(endTime);
        //有效
        param.setTrashFlag(ETrashFlagEnum.VALID.type);
        //收取发送给指定用户
        param.setReceiverType(ESiteLetterReceiverTypeEnum.ONE.type);
        param.setReceiver(userId);
        //已发送状态的
        param.setState(ESiteLetterSateEnum.SENDED.type);
        return dataAccessService.getList(MAPPER + ".getReceiveLetterByTime", param);
    }

    //获取数据列表
    @Log("查询站内信主体列表")
    @Override
    public List<SiteLetter> getList(SiteLetter obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Log("分页查询站内信主体列表")
    @Override
    public List<SiteLetter> getPage(SiteLetter obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Log("查询站内信主体")
    @Override
    public SiteLetter get(Long id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Log("统计站内信主体数量")
    @Override
    public int count(SiteLetter obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Log("修改站内信主体")
    @Override
    public int update(SiteLetter obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Log("批量删除站内信主体")
    @Override
    public int delete(Long[] idArr) {
        int i = 0;
        for(Long id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Log("删除站内信主体")
    @Override
    public int delete(Long id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Log("新增站内信主体")
    @Override
    public int insert(SiteLetter obj) {
        obj.setId(IDGenerator.getId());
        if (obj.getDataSequence() == null) {
            obj.setDataSequence(1L);
        }
        if (obj.getCreateTime() == null) {
            obj.setCreateTime(new Date());
        }
        if (obj.getUpdateTime() == null) {
            obj.setUpdateTime(new Date());
        }
        if (obj.getDataVersion() == null) {
            obj.setDataVersion(1L);
        }
        if (obj.getTrashFlag() == null) {
            obj.setTrashFlag(ETrashFlagEnum.VALID.type);
        }
        if (ESiteLetterSateEnum.SENDED.eq(obj.getState()) && obj.getSendTime() == null) {
            obj.setSendTime(new Date());
        }
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Log("批量新增站内信主体")
    @Override
    public int batchInsert(List<SiteLetter> list) {
        int i = 0;
        for(SiteLetter item : list){
            i += this.insert(item);
        }
        return i;
    }

}